import {
  uploadDocument,
  getPendingDocuments,
  getDocumentsByPage,
  approveDocument,
  rejectDocument,
  downloadTemplate,
  downloadDocument,
  previewDocument,
  getDocumentById
} from '@/api/document'

export const documentService = {
  upload: async (file, seekerName) => {
    try {
      const response = await uploadDocument(file, seekerName)
      return response.data
    } catch (error) {
      const errorMsg = error.response?.data?.msg || error.message || '上传失败'
      throw new Error(errorMsg)
    }
  },

  getPending: async (params) => {
    try {
      const response = await getPendingDocuments(params)
      return response.data
    } catch (error) {
      const errorMsg = error.response?.data?.msg || error.message || '获取数据失败'
      throw new Error(errorMsg)
    }
  },

  getList: async (params) => {
    try {
      const response = await getDocumentsByPage({
        page: params.page || 1,
        pageSize: params.pageSize || 10,
        fileName: params.fileName || '',
        seekerName: params.seekerName || '',
        status: params.status || ''
      })
      return response.data
    } catch (error) {
      const errorData = error.response?.data
      let errorMsg = '获取数据失败'

      if (errorData) {
        if (errorData.msg) {
          errorMsg = errorData.msg
        } else if (errorData.message) {
          errorMsg = errorData.message
        } else if (typeof errorData === 'string') {
          errorMsg = errorData
        }
      } else if (error.message) {
        errorMsg = error.message
      }

      console.error('获取文档列表失败:', error)
      throw new Error(errorMsg)
    }
  },

  approve: async (id, approvedBy) => {
    try {
      const response = await approveDocument(id, approvedBy)
      return response.data
    } catch (error) {
      const errorMsg = error.response?.data?.msg || error.message || '审核失败'
      throw new Error(errorMsg)
    }
  },

  reject: async (id, approvedBy, comments) => {
    try {
      const response = await rejectDocument(id, approvedBy, comments)
      return response.data
    } catch (error) {
      const errorMsg = error.response?.data?.msg || error.message || '驳回失败'
      throw new Error(errorMsg)
    }
  },

  downloadTemplate: async () => {
    try {
      const response = await downloadTemplate()
      return response // 直接返回整个response对象
    } catch (error) {
      const errorMsg = error.response?.data?.msg || error.message || '下载模板失败'
      throw new Error(errorMsg)
    }
  },



    download: async (id) => {
      try {
        const response = await downloadDocument(id);
        // 现在 response 是完整的 AxiosResponse 对象
        return response.data; // 获取 blob 数据
      } catch (error) {
        const errorData = error.response?.data;
        let errorMsg = '下载文件失败';

        if (errorData instanceof Blob) {
          // 如果是 blob 类型的错误响应，尝试读取错误信息
          const text = await errorData.text();
          try {
            const errorJson = JSON.parse(text);
            errorMsg = errorJson.msg || errorJson.message || errorMsg;
          } catch {
            errorMsg = text || errorMsg;
          }
        } else if (errorData) {
          if (errorData.msg) {
            errorMsg = errorData.msg;
          } else if (errorData.message) {
            errorMsg = errorData.message;
          } else if (typeof errorData === 'string') {
            errorMsg = errorData;
          }
        } else if (error.message) {
          errorMsg = error.message;
        }

        console.error('下载文件失败:', error);
        throw new Error(errorMsg);
      }
    },

    preview: async (id) => {
      try {
        const response = await previewDocument(id);
        // 现在 response 是完整的 AxiosResponse 对象
        return response.data; // 获取 blob 数据
      } catch (error) {
        const errorData = error.response?.data;
        let errorMsg = '预览文件失败';

        if (errorData instanceof Blob) {
          // 处理 blob 类型的错误响应
          const text = await errorData.text();
          try {
            const errorJson = JSON.parse(text);
            errorMsg = errorJson.msg || errorJson.message || errorMsg;
          } catch {
            errorMsg = text || errorMsg;
          }
        } else if (errorData) {
          if (errorData.msg) {
            errorMsg = errorData.msg;
          } else if (errorData.message) {
            errorMsg = errorData.message;
          } else if (typeof errorData === 'string') {
            errorMsg = errorData;
          }
        } else if (error.message) {
          errorMsg = error.message;
        }

        console.error('预览文件失败:', error);
        throw new Error(errorMsg);
      }
    },



  getById: async (id) => {
    try {
      const response = await getDocumentById(id)
      return response.data
    } catch (error) {
      const errorMsg = error.response?.data?.msg || error.message || '获取文档详情失败'
      throw new Error(errorMsg)
    }
  }
}
